<?php 
class DomArMultiDbTestCase extends DomArTestUnitTestCase {

	// ---------------------------------------------------------------
	// TESTS
	// ---------------------------------------------------------------
	
	public function testBasics() {
	
		DomArTestClass_MultiDatabaseParent::getTable()->update();
		
		$a = new DomArTestClass_MultiDatabaseParent();
		$b = new DomArTestClass_MultiDatabaseChild();
		$c = new DomArTestClass_MultiDatabaseChild();
		$d = new DomArTestClass_MultiDatabaseChild();

		$a->save();
		$b->save();
		
		$this->assertEqual( DomArTestClass_MultiDatabaseParent::getDbTable(), '`domartestdatabase2`.`parent`');
		$this->assertEqual( DomArTestClass_MultiDatabaseChild::getDbTable(), '`domartestdatabase`.`child`');
		
		$a->child = $b;
		$a->children[] = $c;
		$a->children[] = $d;
		$a->save();
		
		DomAr::flushStoredObjects();
		
		// -----------------------------
		// Basic stuff
		$a = DomArTestClass_MultiDatabaseParent::load( $a->id );
		$this->assertEqual( get_class($a->child), 'DomArTestClass_MultiDatabaseChild' );
		
		$children =  $a->children->query()->commit()->toArray();
		$this->assertEqual( count($children), 2 );
		foreach( $a->children as $child ) {
			$this->assertEqual( get_class($child), 'DomArTestClass_MultiDatabaseChild' );
		}
		
		
		DomAr::flushStoredObjects();
		
		// -----------------------------
		// Test joinProperty query
		$query = DomArTestClass_MultiDatabaseParent::query()
			->joinProperty('child','c')
			->setMainObjectAlias(NULL)
			->commit()->toArray();
	
		$this->assertEqual( count($query), 1 );
		$this->assertEqual( count($query[0]), 2 );
		$this->assertEqual( get_class($query[0]['t']), 'DomArTestClass_MultiDatabaseParent' );
		$this->assertEqual( get_class($query[0]['c']), 'DomArTestClass_MultiDatabaseChild' );
		
	
	} 
	
	public function testDifferentConnections() {
	
		DomArTestClass_MultiDatabaseParent::getTable()->update();
		
		$a = new DomArTestClass_MultiDatabaseParent();
		$b = new DomArTestClass_MultiDatabaseChild();
		$c = new DomArTestClass_MultiDatabaseChild();
		$d = new DomArTestClass_MultiDatabaseChild();

		$a->child = $b;
		$a->children[] = $c;
		$a->children[] = $d;
		$a->save();
		
		DomAr::flushStoredObjects();
		
		DomSql::init('localhost','root','','domartestdatabase2','SECOND_DB');
		DomSql::disconnect(DomSql::DEFAULT_ALIAS);
		
		try {
			$item = DomArTestClass_MultiDatabaseParent::load( $a->id );
			$this->assertTrue(false);
		} catch( Exception $e ) {
			$this->assertTrue(true);
		}
		
		// -----------------------------
		// Test joinProperty query
		$query = DomArTestClass_MultiDatabaseParent::query()
			->setAlias('SECOND_DB')
			->joinProperty('child','c')
			->setMainObjectAlias(NULL)
			->commit()->toArray();

		$this->assertEqual( count($query), 1 );
		$this->assertEqual( count($query[0]), 2 );
		$this->assertEqual( get_class($query[0]['t']), 'DomArTestClass_MultiDatabaseParent' );
		$this->assertEqual( get_class($query[0]['c']), 'DomArTestClass_MultiDatabaseChild' );
		
		DomSql::init('localhost','root','','domartestdatabase');
	
	} 
	
	
}
?>